package com.hxqc.basic.serivce;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hxqc.basic.interceptor.LogAnnotation;
import com.hxqc.basic.mapper.CommonMapper;
import com.hxqc.basic.mapper.MenuMapper;

@Transactional
@Service
public class MenuService {

	@Resource
	private MenuMapper menuMapper;

	@Resource
	private CommonMapper commonMapper;
	
	/**
	 * 获得权限列表
	 * @param map
	 * @return
	 */
	public List<Map<String, Object>> findAllRights(Map<String, String> map) {
		return menuMapper.findAllRights(map);
	}
	
	/**
	 * 菜单权限数
	 * @param map
	 * @return
	 */
	public Integer countAllRights(Map<String, String> map) {
		return menuMapper.countAllRights(map);
	}
	
	/**
	 * 跟新菜单排序
	 * @param option
	 * @param right_id
	 */
	@LogAnnotation("菜单排序")
	@Transactional
	public void orderMenu(String option, String right_id) {

		Map<String, Object> right = menuMapper.findRightById(right_id);
		if (null == right)
			return;
		int sort_id = (Integer) right.get("sort_id");
		Map<String, Object> map = null;
		if (option.equals("TOP")) {
			map = commonMapper.findUniqueResultBySql(
					"select * from sys_right where  sort_id<" + sort_id + " ORDER BY sort_id desc");
			if (null != map) {
				Integer min = (Integer) commonMapper.getIntegerBySql("SELECT min(sort_id) FROM sys_right");
				if (null == min)
					return;
				commonMapper.excuteUpdate("update sys_right set sort_id=sort_id+1 where  sort_id<" + sort_id);
				menuMapper.updateRightOrder(min.intValue(), right_id);
			} else {
				if (sort_id - 1 >= 0) {
					map = commonMapper.findUniqueResultBySql(
							"select * from sys_right where  sort_id=" + sort_id + " and right_id!=" + right_id);
					if (map != null)
						menuMapper.updateRightOrder(sort_id - 1, right_id);
				}
			}

		} else if (option.equals("BOTTOM")) {
			// 置低

			map = commonMapper
					.findUniqueResultBySql("select * from sys_right where  sort_id>" + sort_id + " ORDER BY sort_id  ");
			if (null != map) {
				Integer max = (Integer) commonMapper.getIntegerBySql("SELECT MAX(sort_id) FROM sys_right");
				if (null == max)
					return;
				commonMapper.excuteUpdate("update sys_right set sort_id=sort_id-1 where  sort_id>" + sort_id);

				menuMapper.updateRightOrder(max, right_id);
			} else {
				map = commonMapper.findUniqueResultBySql(
						"select * from sys_right where  sort_id=" + sort_id + " and right_id!=" + right_id);
				if (null != map)
					menuMapper.updateRightOrder(sort_id + 1, right_id);
			}

		} else if (option.equals("UP")) {
			// 向上
			map = commonMapper.findUniqueResultBySql(
					"select * from sys_right where   sort_id<" + sort_id + " ORDER BY sort_id desc");
			if (null != map) {
				menuMapper.updateRightOrder((Integer) map.get("sort_id"), right_id);
				menuMapper.updateRightOrder(sort_id, map.get("right_id").toString());
			} else {
				if (sort_id - 1 >= 0) {
					map = commonMapper.findUniqueResultBySql(
							"select * from sys_right where   sort_id=" + sort_id + " and right_id!=" + right_id);
					if (map != null)
						menuMapper.updateRightOrder(sort_id - 1, right_id);
				}
			}

		} else if (option.equals("DOWN")) {
			map = commonMapper
					.findUniqueResultBySql("select * from sys_right where   sort_id>" + sort_id + " ORDER BY sort_id ");
			if (null != map) {
				menuMapper.updateRightOrder((Integer) map.get("sort_id"), right_id);
				menuMapper.updateRightOrder(sort_id, map.get("right_id").toString());
			} else {
				map = commonMapper.findUniqueResultBySql(
						"select * from sys_right where  sort_id=" + sort_id + " and right_id!=" + right_id);
				if (null != map)
					menuMapper.updateRightOrder(sort_id + 1, right_id);
			}

		}

	}
	
	/**
	 * 更新权限排序值
	 * @param sort_id
	 * @param right_id
	 */
	public void updateRightOrder(int sort_id, String right_id) {
		menuMapper.updateRightOrder(sort_id, right_id);
	}
	
	/**
	 * 通过权限名称查找权限
	 * @param right_name
	 * @return
	 */
	public Map<String, Object> findRightByRightName(String right_name) {
		return menuMapper.findRightByRightName(right_name);
	}
	
	/**
	 * 添加菜单
	 * @param map
	 */
	@LogAnnotation("添加菜单")
	@Transactional
	public void insertMenu(Map<String, String> map) {
		if (map.get("parent_id").equals("0"))
			map.put("access_type", "0");
		menuMapper.insertMenu(map);
		// 添加超级管理员权限
		map.put("role_id", "1");
		menuMapper.insertRoleRight(map);

	}
	
	/**
	 * 查找权限信息
	 * @param right_id
	 * @return
	 */
	public Map<String, Object> findRightById(String right_id) {
		return menuMapper.findRightById(right_id);
	}
	
	/**
	 * 编辑菜单
	 * @param map
	 */
	@LogAnnotation("编辑菜单")
	@Transactional
	public void updateMenu(Map<String, String> map) {
		if (map.get("parent_id").equals("0"))
			map.put("access_type", "0");
		menuMapper.updateMenu(map);
		menuMapper.updateRoleRight(map);
	}
	
	/**
	 * 删除菜单
	 * @param right_id
	 */
	@LogAnnotation("删除菜单")
	@Transactional
	public void deleteMenu(String right_id){
		menuMapper.deleteRight(right_id);
		menuMapper.deleteRoleRight(right_id);
	}
	
	public Integer countSortIdRepart(){
		return menuMapper.countSortIdRepart();
	}
	
	public void initMenuSorting(){
		List<Map<String,Object>> list = menuMapper.findAllRights(null);
		if(list.size()>0)
			menuMapper.batchUpdateSort(list);
	}
}
